// 单按钮弹窗
Component({
  properties: {
    modalHidden: {    //弹窗显示与隐藏
      type: Boolean,
      value: true
    },
    config: {
      type: Object,
      value: {
        "scheme_status": 1,
        "scheme_id": '',
        "order_id": '',
        "order_status": '',
        "schemetype":2,   //方案类型  1:手术方案  2:产品方案  3:分割方案
        "index":0
      }
    },
    schemeinfo:{
      type: Array,
      value: []
    }
  },
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  data: {
    // 这里是一些组件内部数据  
    contentConfig: {
      scoreArray: [
        {
          code: 'design_scheme',
          title: '设计方案',
          allstars: 5,
          selected: 0,
          index: 0
        },
        {
          code: 'work_efficiency',
          title: '工作效率',
          allstars: 5,
          selected: 0,
          index: 1
        },
        {
          code: 'service_attitude',
          title: '服务态度',
          allstars: 5,
          selected: 0,
          index: 2
        }
      ]
    },
    supDescribe:''    //补充描述
  },
  created: function () {
    wx.eventBus.on('getSupDescribe', (re) => {
      this.setData({
        contentConfig: re.contentConfig,
        supDescribe: re.supDescribe
      })
      console.log('新数据',this.data)
    })
  },
  methods: {
    //审核弹窗
    auditWindow:function(){
      wx.showActionSheet({
        itemList: ['不通过', '通过'],
        itemColor:'#4121C4',
        success:  (res) => {
          console.log(res.tapIndex)
          if (res.tapIndex == 0){
            this._refuse()
          }
          if (res.tapIndex == 1) {
            let ok = 0;
            for (var i = 0; i < this.data.schemeinfo.length; i++) {   //判断另一个方案是否都审核通过
              var el = this.data.schemeinfo[i];
              if (el.scheme_status == 1) {
                ok++;
              }
            }
            if (ok == this.data.schemeinfo.length - 1) {    //另一个方案通过则弹出评价
              this.setData({
                modalHidden: false
              })
            }else{
              this._adopt()
            }
          }
        },
        fail: (res) => {
          console.log(res.errMsg)
        }
      })
    },
    /**
   * 不通过
   */
    _refuse: function () {
      wx.showModal({
        title: '提示',
        cancelColor: '#4121C4',
        confirmColor: '#4121C4',
        content: '是否确定拒绝此方案',
        success: (res) => {
          if (res.confirm) {
            var config = this.data.config
            config.scheme_status=2
            this.setData({
              config: config
            })
            this.updateShemeInfo(this.data.config);
          }
        }
      })
    },
    /**
     * 通过
     */
    _adopt: function () {
      let self=this
      wx.showModal({
        title: '提示',
        cancelColor: '#4121C4',
        confirmColor: '#4121C4',
        content: '是否确定通过此方案',
        success:(res)=> {
          if (res.confirm) {
            // let ok = 0;
            // for (var i = 0; i < self.data.schemeinfo.length; i++) {
            //   var el = self.data.schemeinfo[i];
            //   if (el.scheme_status == 1) {
            //     ok++;
            //   }
            // }
            // if(ok){

            // }
            // self.setData({
            //   modalHidden: false
            // })
            // if (!self.data.modalHidden) {
            //   self.updateShemeInfo(self.data.config);
            // }
            self.updateShemeInfo(self.data.config);
            console.log('用户点击确定')
          }
        }
      })
    },
    //文件审核通过改变状态
    updateShemeInfo: function (data) {
      wx.request({
        url: wx.envConfig.host + '/order/updateSchemeStatus?params=' + JSON.stringify(data) + '&cookiez=' + wx.getStorageSync('cookiez'),
        method: 'POST',
        success: (res) => {
          if (res.data.error_code === "Y10000") {
            //全局更新数据
            wx.eventBus.trigger('getSchemeInfo', this.data.config.index)
          }
        },
        fail: function ({ errMsg }) {
          console.log('request fail', errMsg)
        }
      })
    },
    //关闭回调
    _closeback: function () {
      
    },
    //提交评价
    _submit:function(){
      let self=this
      var data = {
        "scheme_status": 1,
        "scheme_id": self.data.config.scheme_id,
        "order_id": self.data.config.order_id,
        "order_status": self.data.config.order_status,
        'evaluate_params': {
          'design_scheme': self.data.contentConfig.scoreArray[0].selected,
          'service_attitude': self.data.contentConfig.scoreArray[2].selected,
          'user_remarks': self.data.supDescribe,
          'work_efficiency': self.data.contentConfig.scoreArray[1].selected
        }
      }
      wx.request({
        url: wx.envConfig.host + '/order/updateSchemeStatus?params=' + JSON.stringify(data) + '&cookiez=' + wx.getStorageSync('cookiez'),
        method: 'POST',
        success:  (res) => {
          if (res.data.error_code === "Y10000") {
            self.setData({
              modalHidden:true
            })
            self.updateShemeInfo(this.data.config)
            wx.eventBus.trigger('getSchemeInfo', res.data)
          }
        },
        fail: function ({ errMsg }) {
          console.log('request fail', errMsg)
        }
      })
    }
  }
})  